<?php

class ProfileController extends Controller
{

    public function actionIndex()
    {	
	$userModel = FactoryService::get('user')->get(Yii::app()->user->getId());
	if (Yii::app()->getRequest()->getIsPostRequest()) {
	    $errors = false;
	    $user = Yii::app()->getRequest()->getParam('user');
	    foreach ($user as $key => $value) {
		$user[$key] = PurifierManager::getInstance()->filter($value);
	    }
	    $birthday = Yii::app()->getRequest()->getParam('birthday');
	    $flag = true;
	    foreach ($birthday as $item) {
		if (!intval($item)) {
		    $flag = false;
		}
	    }
	    if ($flag) {
		$date = implode('-', array($birthday['year'], $birthday['month'], $birthday['day']));
		$user['profile_birthday'] = $date;
	    }
	    $avatar = CUploadedFile::getInstanceByName('profile_avatar');
	    if ($avatar) {
		if ($avatar->getSize() > Yii::app()->params['maxUploadSize']) {
		    $errors['sizeErr'] = 'Размер файла больше допустимого';
		}

		if (!in_array(strtolower($avatar->getExtensionName()), Yii::app()->params['allowedTypes'])) {
		    $errors['sizeErr'] = 'Недопустимый тип загружаемого файла';
		}
		if (!$errors) {
		    $path = $userModel->getUploadPath(true) . DIRECTORY_SEPARATOR . 'avatar' . DIRECTORY_SEPARATOR;
		    if (!file_exists($path)) {
			mkdir($path, 0777, true);
		    }
		    $filename = Yii::app()->params['avatar']['width'] . '_' . Yii::app()->params['avatar']['height'] . '.' . $avatar->getExtensionName();
		    $thumbName = Yii::app()->params['avatar']['thumbWidth'] . '_' . Yii::app()->params['avatar']['thumbHeight'] . '.' . $avatar->getExtensionName();
		    if ($avatar->saveAs($path . $filename)) {
			$file = Yii::app()->image->load($path . $filename);
			$file->resize(Yii::app()->params['avatar']['width'], Yii::app()->params['avatar']['height'])->save();
			$file->resize(Yii::app()->params['avatar']['thumbWidth'], Yii::app()->params['avatar']['thumbHeight'])->save($path . $thumbName);
			$user['avatar_ext'] = $avatar->getExtensionName();
		    }
		}
		else {
		    Yii::app()->user->setFlash('profileErr', implode('<br />', $errors));
		}
	    }	    
	    $userModel->setDbData((object) $user);
	    if (FactoryService::get('user')->save($userModel)) {
		Yii::app()->user->setFlash('profileErr#', 'Профиль сохранен');
	    }
	}
	$this->render('index', array('model' => $userModel));
    }

    public function actionView()
    {
	$id = Yii::app()->getRequest()->getParam('id');
	if (!$id) {
	    throw new CHttpException(404, 'Тут пусто');
	}
	$userModel = FactoryService::get('user')->get($id);
	if (!$userModel) {
	    throw new CHttpException(404, 'Тут пусто');
	}
	$this->breadcrumbs = array(
	    'Профиль пользователя ' . $userModel->getProfileName()
	);
	SeoManager::getInstance()->set(array(
	    'title' => 'Профиль пользователя ' . $userModel->getProfileName(),
	    'description' => 'Профиль пользователя ' . $userModel->getProfileName(),
	    'keywords' => 'Профиль пользователя ' . $userModel->getProfileName(),
	));
	$this->render('view', array('model' => $userModel));
    }

    public function actionList()
    {
	$type = Yii::app()->getRequest()->getParam('type');
	$this->_getTypeList($type);	
    }
    
    protected function _getTypeList($type)
    {		
	if (!$type) {
	    return false;
	}
	$userModel = FactoryService::get('user')->get(Yii::app()->user->getId());
	$page = Yii::app()->getRequest()->getParam('page', 1);
	$pager = new CPagination(FactoryService::get($type)->getCountByUser($userModel));
	$pager->setPageSize(Yii::app()->params['perPage']);
	
	FactoryService::get($type)->getByUser($userModel, $page, Yii::app()->params['perPage']);
	
	$this->breadcrumbs = array(
	    'Профиль' => $this->createUrl('/profile'),
	    'Персональные данные'
	);

	$this->render($type, array(
	    'model' => $userModel,
	    'pager' => $pager,
	    'page' => $page,
	    'perPage' => Yii::app()->params['perPage']
		)
	);
    }

}
?>

